Twitter

Reference material for the following tutorial

Setup a Twitter account and create an App

  1. Setup your username and password at https://twitter.com/.
  2. Go to https://apps.twitter.com/ and sign in with your username and password
  3. Click on Create a new App
Create a Twitter app at https://apps.twitter.com/.

Create a Twitter app at https://apps.twitter.com/.

  1. Fill in with you App name, brief description and a website. Ideally this website is where people can find more information about your app. However, if you don’t have one just insert a valid link to a web page.

  2. As callback URL set: http://127.0.0.1:1410

Setup your app information andcallback URL.

Setup your app information andcallback URL.

  1. Once you click on Create your Twitter Application you will be re-directed to your application page. You’re almost there! Go to the Keys and Access Tokens tab and at the bootom of this page click on Create my access token. Your access token will appear at the bottom of the page.
Create access token to connect R with Twitter.

Create access token to connect R with Twitter.

  1. Now you have everything you need to connect R with Twitter! From your application page you need four things:
  • API key
  • API secret
  • Token
  • Token secret
Create access token to connect R with Twitter.

Create access token to connect R with Twitter.

Connect your R session with Twitter

There are two well-known packages used to collect data from Twitter directly into R:

Setup your R session to download Twitter data with the twitteR package

The function twitteR::searchTwitter will return a list of tweets which you can easily coherce to standard data.frame with the function twitteR::twListToDF.

> api_key <- "your_api_key"
> api_secret <- "your_api_secret"
> token <- "your_token"
> token_secret <- "your_token_secret"
> 
> setup_twitter_oauth(api_key, api_secret, token, token_secret)
> 
> # Search tweets
> MyTweets <- searchTwitter("#MyChosenHastags", lang = "en")

Setup your R session to download Twitter data with the rtweet package

Note: Remember to set set_renv=FALSE when you run the rtweet::create_token() function. The default should be set to FALSE as mentioned in the help page ?rtweet but it is instead set to TRUE. If set_renv=TRUE it will save a hidden token named .rtweet_token.rds which will be used as the default evironemnt twietter token variable.

More information about this can be found at Obtaining and using access tokens.

The function rtweet::search_tweets returns tweets already as a data frame.

> appname <- "Rladies_app"
> api_key <- "your_api_key"
> api_secret <- "your_api_secret"
> 
> setup_twitter_oauth(api_key, api_secret, token, token_secret)
> 
> ## create token named 'twitter_token'
> twitter_token <- create_token(app = appname, consumer_key = key, consumer_secret = secret)
> 
> MyTweet <- search_tweets("#MyChosenHastags", lang = "en", token = twitter_token)

Facebook

> install.packages("devtools")
> install_github("pablobarbera/Rfacebook", subdir = "Rfacebook")
> install.packages("httr")
> library(devtools)
> library(Rfacebook)
> library(httr)
> library(jsonlite)

Option 1: connect via the facebook API

> app_id1 <- "useYours"
> app_secret1 <- "useYous"
> 
> fb_oauth <- fbOAuth(app_id = app_id1, app_secret = app_secret1, extended_permissions = FALSE)
> facebook <- oauth_endpoint(authorize = "https://www.facebook.com/dialog/oauth", 
+     access = "https://graph.facebook.com/oauth/access_token")
  • Credentials you get from registering a new application

  • OAuth endpoints given in the Facebook API documentation
  1. authorization_base_url = ‘https://www.facebook.com/dialog/oauth
  2. token_url = ‘https://graph.facebook.com/oauth/access_token
  3. redirect_uri = ‘http://localhost:1410/’, this should match Site URL
> app_name <- "useR2018"  #not used but good for organisation
> client_id = app_id1
> client_secret = app_secret1
> 
> myapp <- oauth_app(app_name, client_id, client_secret)
> 
> 
> facebook_oauth <- oauth2.0_token(facebook, myapp)
> save(facebook_oauth, file = "fb_oauth")
> 
> load("fb_oauth")
> 
> token <- facebook_oauth$credentials$access_token
> 
> me <- getUsers("me", token = fb_oauth)
> 
> me2 <- getUsers("me", token = token)
> 
> # getUsers(c('barackobama', 'donaldtrump'), token) getUsers(c('barackobama',
> # 'donaldtrump'), fb_oauth)

Option 2: connect via the authentication token

Instagram

> install.packages("httpuv")
> install.packages("httr")
> install.packages("jsonlite")
> install.packages("RCurl")
> require(httr)
> require(jsonlite)
> require(RCurl)
> full_url <- oauth_callback()
> full_url <- gsub("(.*localhost:[0-9]{1,5}/).*", x = full_url, replacement = "\\1")
> print(full_url)
> 
> # http://localhost:1410/
> 
> # register your client in Instagram Parameters to use
> 
> # define 4 variables get the first 3 from the instagram setup
> app_name <- "useYour"
> client_id <- "useYour"
> client_secret <- "useYour"
> 
> # set the type of access
> scope = "public_content"
> 
> # set access points for authorization
> # https://www.instagram.com/developer/endpoints/users/
> instagram <- oauth_endpoint(authorize = "https://api.instagram.com/oauth/authorize", 
+     access = "https://api.instagram.com/oauth/access_token")
> 
> # the application that will be used to access Ig
> myapp <- oauth_app(app_name, client_id, client_secret)
> 
> # authentication
> 
> # ig_oauth <- oauth2.0_token(instagram, myapp,scope='basic', type =
> # 'application/x-www-form-urlencoded',cache=FALSE)
> 
> ig_oauth <- oauth2.0_token(instagram, myapp, scope = "basic")
> 
> 
> save(ig_oauth, file = "ig_oauth")
> load("ig_oauth")
> 
> token <- ig_oauth$credentials$access_token